package org.september.pisces.filestore.controller;

import java.io.File;

import org.apache.commons.io.FileUtils;
import org.september.core.exception.BusinessException;
import org.september.pisces.filestore.entity.FileStoreItem;
import org.september.pisces.filestore.entity.FileStorePool;
import org.september.pisces.filestore.service.FileStoreManager;
import org.september.pisces.user.permission.service.OperationLogService;
import org.september.simpleweb.controller.BaseController;
import org.september.simpleweb.model.ResponseVo;
import org.september.smartdao.common.BaseService;
import org.september.smartdao.model.Page;
import org.september.smartdao.model.ParamMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import jakarta.validation.Valid;

/**
 * 文件存储池管理
 */
@Controller
public class FileStorePoolController extends BaseController {

	@Autowired
	private BaseService baseService;

    @Autowired
    private OperationLogService operationLogService;

    @Autowired
    private FileStoreManager fileStoreManager;

    @RequestMapping(value = List_Page)
    public ModelAndView poolList() throws Exception {
        ModelAndView mv = new ModelAndView();
        return mv;
    }

    @ResponseBody
    @RequestMapping(value = List_Data)
    public ResponseVo<Page<FileStorePool>> listPoolData(Page<FileStorePool> page, FileStorePool vo) throws Exception {
        ParamMap pm = new ParamMap(vo);
        page = baseService.getCommonDao().findPageByParams(FileStorePool.class, page, "PiscesFileStorePool.listPoolData", pm);
        return ResponseVo.<Page<FileStorePool>>BUILDER().setData(page).setCode(ResponseVo.BUSINESS_CODE.SUCCESS);
    }

    @RequestMapping(value = Add_Page)
    public ModelAndView addPool() throws Exception {
        ModelAndView mv = new ModelAndView();
        return mv;
    }

    @ResponseBody
    @RequestMapping(value = Add_Action)
    public ResponseVo<String> doAddPool(FileStorePool vo) throws Exception {
    	baseService.save(vo);
//    	fileStoreWebMvcConfigurerAdapter.addResourceLocation(vo.getPoolName(), vo.getDiskPath());
        operationLogService.addLog("添加了存储池【" + vo.getId() + "】");
        return ResponseVo.<String>BUILDER().setData("添加文件存储池需要重启应用后生效").setCode(ResponseVo.BUSINESS_CODE.SUCCESS);
    }

    @RequestMapping(value = Edit_Page)
    public ModelAndView editPool(Long id) throws Exception {
        ModelAndView mv = new ModelAndView();
        FileStorePool po = baseService.getCommonDao().get(FileStorePool.class, id);
        mv.addObject("pool", po);
        return mv;
    }

    @ResponseBody
    @RequestMapping(value = Edit_Action)
    public ResponseVo<String> doUpdatePool(@Valid FileStorePool po) throws Exception {
    	baseService.update(po);
        operationLogService.addLog("更新了存储池【" + po.getPoolName() + "】");
        return ResponseVo.<String>BUILDER().setData("").setCode(ResponseVo.BUSINESS_CODE.SUCCESS);
    }

    @ResponseBody
    @RequestMapping(value = Delete_Action)
    public ResponseVo<String> delete(Long id) throws Exception {
    	FileStorePool po = baseService.getCommonDao().get(FileStorePool.class, id);
        if (po == null) {
            throw new BusinessException("数据不存在或已删除");
        }
        baseService.delete(po);
        operationLogService.addLog("删除了存储池【" + po.getPoolName() + "】");
        return ResponseVo.<String>BUILDER().setCode(ResponseVo.BUSINESS_CODE.SUCCESS);
    }

//    @ResponseBody
//    @RequestMapping(value = "/test")
//    public ResponseVo<String> test() throws Exception {
//    	FileStoreItem item = fileStoreManager.save("pool", FileUtils.readFileToByteArray(new File("D:\\work\\文档\\交通\\traffic\\项目信息\\安徽\\安庆\\两站一场\\报名资料\\两站一场视频\\10app预警列表.mp4")),".mp4");
//        return ResponseVo.<String>BUILDER().setCode(ResponseVo.BUSINESS_CODE.SUCCESS).setData(item.getPath());
//    }

    private static final String Mapping_Prefix = "/pisces/fileStore/pool";
	public static final String List_Page = Mapping_Prefix+"/poolList";
	public static final String List_Data = Mapping_Prefix+"/listPoolData";

	public static final String Add_Page = Mapping_Prefix+"/addPool";
	public static final String Add_Action = Mapping_Prefix+"/doAddPool";
	public static final String Edit_Page = Mapping_Prefix+"/editPool";
	public static final String Edit_Action = Mapping_Prefix+"/doUpdatePool";

	public static final String Delete_Action = Mapping_Prefix+"/delete";
}
